package com.zl.servlet;

import com.zl.entity.Employee;
import com.zl.service.EmployeeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/employee")
public class EmployeeServlet extends HttpServlet {
    //调用业务层
    private EmployeeService employeeService=new EmployeeService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //指定输出到浏览器得类型
        response.setContentType("text/json;charset=utf-8");
        //通过打印流进行输出
        PrintWriter out=response.getWriter();
        //得到所有对象集合
        List<Employee> employees=employeeService.getAll();
        //把数据转换成json对象输出到浏览器上
        String toJson=employeeService.toJson(employees);
        out.print(toJson);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     this.doPost(request,response);
    }
}
